﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

using CNCSoft.EWinform;
using Common;

namespace CNCSoft.ErpApps.TMS
{
    public partial class txtChonTuDanhMuc : TextBox
    {
        private string strRefField;
        private string strRefTable;

        // Declares the name and type of the property.
        public string RefField
        // Retrieves the value of the private variable colBColor.
        {
            get
            {
                return strRefField;
            }
            // Stores the selected value in the private variable colBColor, and 
            // updates the backcolor of the label control lblDisplay.
            set
            {
                strRefField = value;
                //txtValue.Text = strRefField;
            }
        }

        public string RefTable
        // Retrieves the value of the private variable colBColor.
        {
            get
            {
                return strRefTable;
            }
            // Stores the selected value in the private variable colBColor, and 
            // updates the backcolor of the label control lblDisplay.
            set
            {
                strRefTable = value;
            }
        }


        public txtChonTuDanhMuc()
        {
            InitializeComponent();
        }

        private void txtValue_Validated(object sender, EventArgs e)
        {
            if (strRefField == "" || strRefField == null) strRefField =this.Name.Substring(3)  ;

            if (strRefTable == "" || strRefTable == null)   //Khong truyen DanhMuc, coi thu neu co MA_... thi lay DM_...
            {
                if (strRefField.ToUpper().Substring(0, 2) == "MA")
                {
                    strRefTable = strRefField.ToUpper().Replace("MA", "DM_");
                }
            }

            //MessageBox.Show(strRefField + "  trong " + strRefTable);
            //Khong co trong danh muc la hien list data
            try  //Neu ko co se xuong catch
            {
                string str = CNCSoft.EData.SQLHelper.ExecuteScalar(GlobalVariable.WSInfo.ConnectionString,
                            "SELECT " + strRefField + " FROM " + strRefTable + " WHERE " + strRefField + " = '" + this.Text + "'").ToString();
            }
            catch (Exception ex)
            {
                //Can xu ly them truong hop khong co DanhMuc (do ngo nhan ten MA...)
                if (ex.Message.IndexOf("'" + strRefTable + "'") < 0)  //Bao loi lien quan den Danh Muc (Invalid Object name <DM>). Khong co DanhMuc thi thoi, ko load data list lam gi
                {
                    if (strRefTable != "" && strRefTable != null)
                        this.Text = LoadListData("SELECT * FROM " + strRefTable);
                    return;
                }
            }
            

        }


        private string LoadListData(string strSELECT)
        {
            try
            {
                FormView fv = null;
                fv = new FormView(strSELECT, GlobalVariable.WSInfo.ConnectionString, GlobalVariable.WSInfo.WorkingDate);
                //fv.DateFilterColumns = new string[,] { { "NgayDeNghi", "Ngày yêu cầu" } };
                //fv.CollumnFocusInFilterGrid = 3;

                fv.ShowDialog(this);
                if (fv.DialogResult == DialogResult.OK)
                {
                    return fv.SelectedIDs.Replace("'", "").Replace("(", "").Replace(")", "");
                }
                else
                    return "";
            }
            catch
            {
                return "";
            }
        }

        private void txtChonTuDanhMuc_Validated(object sender, EventArgs e)
        {
            if (strRefField == "" || strRefField == null) strRefField = this.Name.Substring(3);

            if (strRefTable == "" || strRefTable == null)   //Khong truyen DanhMuc, coi thu neu co MA_... thi lay DM_...
            {
                if (strRefField.ToUpper().Substring(0, 2) == "MA")
                {
                    strRefTable = strRefField.ToUpper().Replace("MA", "DM_");
                }
            }

            //MessageBox.Show(strRefField + "  trong " + strRefTable);
            //Khong co trong danh muc la hien list data
            try  //Neu ko co se xuong catch
            {
                string str = CNCSoft.EData.SQLHelper.ExecuteScalar(GlobalVariable.WSInfo.ConnectionString,
                            "SELECT " + strRefField + " FROM " + strRefTable + " WHERE " + strRefField + " = '" + this.Text + "'").ToString();
            }
            catch (Exception ex)
            {
                //Can xu ly them truong hop khong co DanhMuc (do ngo nhan ten MA...)
                if (ex.Message.IndexOf("'" + strRefTable + "'") < 0)  //Bao loi lien quan den Danh Muc (Invalid Object name <DM>). Khong co DanhMuc thi thoi, ko load data list lam gi
                {
                    if (strRefTable != "" && strRefTable != null)
                        this.Text = LoadListData("SELECT * FROM " + strRefTable);
                    return;
                }
            }
        }

        private void txtChonTuDanhMuc_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                txtChonTuDanhMuc_Validated(sender, e);
            }
            
            if (e.KeyCode == Keys.F3)
            {
                try  //Neu ko co se xuong catch
                {
                    string str = CNCSoft.EData.SQLHelper.ExecuteScalar(GlobalVariable.WSInfo.ConnectionString,
                                "SELECT " + strRefField + " FROM " + strRefTable + " WHERE " + strRefField + " = '" + this.Text + "'").ToString();
                }
                catch (Exception ex)
                {
                    //Can xu ly them truong hop khong co DanhMuc (do ngo nhan ten MA...)
                    if (ex.Message.IndexOf("'" + strRefTable + "'") < 0)  //Bao loi lien quan den Danh Muc (Invalid Object name <DM>). Khong co DanhMuc thi thoi, ko load data list lam gi
                    {
                        if (strRefTable != "" && strRefTable != null)
                            this.Text = LoadListData("SELECT * FROM " + strRefTable);
                        return;
                    }
                }
            }
        }

        
    }
}
